import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MallBrokerageUserApi } from '#/api/mall/trade/brokerage/user';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { fenToYuan } from '@vben/utils';

import { getRangePickerDefaultProps } from '#/utils';

import { $t } from '#/locales';

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'bindUserId',
      label: $t('views.mall.trade.brokerage.user.bindUserId'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.mall.trade.brokerage.user.bindUserIdPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'brokerageEnabled',
      label: $t('views.mall.trade.brokerage.user.brokerageEnabled'),
      component: 'Select',
      componentProps: {
        placeholder: $t('views.mall.trade.brokerage.user.brokerageEnabledPlaceholder'),
        allowClear: true,
        options: [
          { label: $t('common.enabled'), value: true },
          { label: $t('common.disabled'), value: false },
        ],
      },
      defaultValue: true,
    },
    {
      fieldName: 'createTime',
      label: $t('common.createTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(
  onBrokerageEnabledChange?: (
    newEnabled: boolean,
    row: MallBrokerageUserApi.BrokerageUser,
  ) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'id',
      title: $t('views.common.id'),
      minWidth: 80,
    },
    {
      field: 'avatar',
      title: $t('views.mall.trade.brokerage.user.avatar'),
      minWidth: 70,
      cellRender: {
        name: 'CellImage',
      },
    },
    {
      field: 'nickname',
      title: $t('views.mall.trade.brokerage.user.nickname'),
      minWidth: 80,
    },
    {
      field: 'brokerageUserCount',
      title: $t('views.mall.trade.brokerage.user.brokerageUserCount'),
      minWidth: 160,
    },
    {
      field: 'brokerageOrderCount',
      title: $t('views.mall.trade.brokerage.user.brokerageOrderCount'),
      minWidth: 160,
    },
    {
      field: 'brokerageOrderPrice',
      title: $t('views.mall.trade.brokerage.user.brokerageOrderPrice'),
      minWidth: 160,
      formatter: ({ row }) => `￥${fenToYuan(row.brokerageOrderPrice)}`,
    },
    {
      field: 'withdrawPrice',
      title: $t('views.mall.trade.brokerage.user.withdrawPrice'),
      minWidth: 110,
      formatter: ({ row }) => `￥${fenToYuan(row.withdrawPrice)}`,
    },
    {
      field: 'withdrawCount',
      title: $t('views.mall.trade.brokerage.user.withdrawCount'),
      minWidth: 120,
    },
    {
      field: 'price',
      title: $t('views.mall.trade.brokerage.user.price'),
      minWidth: 100,
      formatter: ({ row }) => `￥${fenToYuan(row.price)}`,
    },
    {
      field: 'frozenPrice',
      title: $t('views.mall.trade.brokerage.user.frozenPrice'),
      minWidth: 100,
      formatter: ({ row }) => `￥${fenToYuan(row.frozenPrice)}`,
    },
    {
      field: 'brokerageEnabled',
      title: $t('views.mall.trade.brokerage.user.brokerageEnabled'),
      minWidth: 130,
      align: 'center',
      cellRender: {
        attrs: { beforeChange: onBrokerageEnabledChange },
        name: 'CellSwitch',
        props: {
          checkedValue: true,
          uncheckedValue: false,
          checkedChildren: '有',
          unCheckedChildren: '无',
        },
      },
    },
    {
      field: 'brokerageTime',
      title: $t('views.mall.trade.brokerage.user.brokerageTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'bindUserId',
      title: $t('views.mall.trade.brokerage.user.bindUserId'),
      minWidth: 150,
    },
    {
      field: 'bindUserTime',
      title: $t('views.mall.trade.brokerage.user.bindUserTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 150,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 创建分销员表单配置 */
export function useCreateFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'userId',
      label: $t('views.mall.trade.brokerage.user.userCreate.brokerageUserId'),
      component: 'InputSearch',
      rules: 'required',
    },
    {
      fieldName: 'bindUserId',
      label: $t('views.mall.trade.brokerage.user.userCreate.bindUserId'),
      component: 'InputSearch',
      rules: 'required',
    },
  ];
}

/** 修改分销用户表单配置 */
export function useUpdateFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'bindUserId',
      label: $t('views.mall.trade.brokerage.user.userCreate.bindUserId'),
      component: 'InputSearch',
      rules: 'required',
    },
  ];
}

/** 用户列表弹窗搜索表单配置 */
export function useUserListFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'level',
      label: $t('views.mall.trade.brokerage.user.level'),
      component: 'Select',
      componentProps: {
        options: [
          { label: $t('views.mall.trade.brokerage.user.levelOptions.level-0'), value: undefined },
          { label: $t('views.mall.trade.brokerage.user.levelOptions.level-1'), value: '1' },
          { label: $t('views.mall.trade.brokerage.user.levelOptions.level-2'), value: '2' },
        ],
        allowClear: true,
      },
    },
    {
      fieldName: 'bindUserTime',
      label: $t('views.mall.trade.brokerage.user.bindUserTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 用户列表弹窗表格列配置 */
export function useUserListColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'id',
      title: $t('views.common.id'),
      minWidth: 80,
    },
    {
      field: 'avatar',
      title: $t('views.mall.trade.brokerage.user.avatar'),
      minWidth: 70,
      cellRender: {
        name: 'CellImage',
        props: {
          width: 24,
          height: 24,
          shape: 'circle',
        },
      },
    },
    {
      field: 'nickname',
      title: $t('views.mall.trade.brokerage.user.nickname'),
      minWidth: 80,
    },
    {
      field: 'brokerageUserCount',
      title: $t('views.mall.trade.brokerage.user.brokerageUserCount'),
      minWidth: 80,
    },
    {
      field: 'brokerageOrderCount',
      title: $t('views.mall.trade.brokerage.user.brokerageOrderCount'),
      minWidth: 110,
    },
    {
      field: 'brokerageEnabled',
      title: $t('views.mall.trade.brokerage.user.brokerageEnabled'),
      minWidth: 80,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
      },
    },
    {
      field: 'bindUserTime',
      title: $t('views.mall.trade.brokerage.user.bindUserTime'),
      width: 180,
      formatter: 'formatDateTime',
    },
  ];
}

/** 推广订单列表弹窗搜索表单配置 */
export function useOrderListFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'sourceUserLevel',
      label: $t('views.mall.trade.brokerage.user.level'),
      component: 'Select',
      componentProps: {
        options: [
          { label: $t('views.mall.trade.brokerage.user.levelOptions.level-0'), value: 0 },
          { label: $t('views.mall.trade.brokerage.user.levelOptions.level-1'), value: 1 },
          { label: $t('views.mall.trade.brokerage.user.levelOptions.level-2'), value: 2 },
        ],
      },
      defaultValue: 0,
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'Select',
      componentProps: {
        placeholder: $t('common.statusPlaceholder'),
        allowClear: true,
        options: getDictOptions(DICT_TYPE.BROKERAGE_RECORD_STATUS, 'number'),
      },
    },
    {
      fieldName: 'createTime',
      label: $t('common.createTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 推广订单列表弹窗表格列配置 */
export function useOrderListColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'bizId',
      title: $t('views.mall.trade.brokerage.user.orderList.bizId'),
      minWidth: 80,
    },
    {
      field: 'sourceUserId',
      title: $t('views.mall.trade.brokerage.user.orderList.sourceUserId'),
      minWidth: 80,
    },
    {
      field: 'sourceUserAvatar',
      title: $t('views.mall.trade.brokerage.user.avatar'),
      minWidth: 70,
      cellRender: {
        name: 'CellImage',
        props: {
          width: 24,
          height: 24,
        },
      },
    },
    {
      field: 'sourceUserNickname',
      title: $t('views.mall.trade.brokerage.user.nickname'),
      minWidth: 80,
    },
    {
      field: 'price',
      title: $t('views.mall.trade.brokerage.user.orderList.price'),
      minWidth: 100,
      formatter: ({ row }) => `￥${fenToYuan(row.price)}`,
    },
    {
      field: 'status',
      title: $t('common.status'),
      minWidth: 85,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.BROKERAGE_RECORD_STATUS },
      },
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      width: 180,
      formatter: 'formatDateTime',
    },
  ];
}
